{{- /*
  Complements the section-specific sidebar navigation.
  - Renders the main navigation for site sections, linking to the current or ancestor section/page.
  - Uses the `site.Menus.main` configuration to determine primary navigation structure.
  - Toggles visibility of nested menu items for the main sections.
*/ -}}
<!-- Main navigation for the sidebar -->
<div class="py-2 px-4" x-data="{ expanded: false }">
  <div class="flex w-full items-center justify-between">
    <!-- Current section: use menu, fall back to current section or page -->
    {{- $curr := .FirstSection }}
    {{- if eq $curr site.Home }}
      {{- $curr = . }}
    {{- end }}
    {{- range site.Menus.main }}
      {{- if or (.Page.IsAncestor page) (eq .Page page) }}
        {{- $curr = .Page }}
      {{- end }}
    {{- end }}
    <a class="hover:text-blue-light dark:hover:text-blue-dark" href="{{ $curr.Permalink }}">
      {{- with $curr.Params.icon }}
        <span class="pr-2 icon-sm icon-svg">
          {{- partialCached "icon.html" . . -}}
        </span>
      {{- end }}
      {{- $curr.LinkTitle -}}
    </a>
    <button @click="expanded = !expanded" class="rounded hover:bg-gray-light-300 hover:dark:bg-gray-dark-300">
      <span x-show="! expanded" class="icon-svg">
        {{ partialCached "icon" "arrow_drop_down" "arrow_drop_down" }}
      </span>
      <span x-cloak x-show="expanded" class="icon-svg">
        {{ partialCached "icon" "arrow_drop_up" "arrow_drop_up" }}
      </span>
    </button>
  </div>
  <ul x-cloak x-show="expanded" class="pt-4 space-y-4">
    {{ range site.Menus.main }}
      {{ if ne page.FirstSection .Page }}
      <li>
        <a class="hover:text-blue-light dark:hover:text-blue-dark" href="{{ .URL }}">
          {{- with .Page.Params.icon }}
            <span class="pr-2 icon-sm icon-svg">
              {{- partialCached "icon.html" . . -}}
            </span>
          {{- end }}
          {{- .Name }}
        </a>
      </li>
      {{ end }}
    {{ end }}
  </ul>
</div>
<hr class="m-2 text-gray-light-200 dark:text-gray-dark-300" />
